/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 
 * <p>
 * 文件名:SysRoleController.java
 * <p>
 * 类产生时间: 2012-6-4 下午1:58:51
 * <p>
 * 负责人: 
 * <p>
 * Email:
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hui.platform.system.Constants;
import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.common.util.encrypt.Blowfish;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.constant.ConstantMap;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.domain.SysOperLog;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.service.ISysDeptService;
import com.oceansoft.core.service.ISysUserService;

/**
 * 用户管理控制器
 * 
 * @author yuc,chenw
 * 
 */
@Controller
@RequestMapping("/user")
public class UserManagerController {
	@Resource
	private ISysUserService sysUserService;
	@Resource
	private ISysDeptService sysDeptService;

	@RequestMapping("/list/{deptId}/{pageIndex}.html")
	public String getAllUser(HttpServletRequest request, SysUser user,
			ModelMap model, @PathVariable("deptId") String deptId,
			@PathVariable("pageIndex") int pageIndex) {
		model.addAttribute("deptId", deptId);
		model.addAttribute("user", user);
		return "/dept/user_list";
	}

	/**
	 * 跳转到新增
	 */
	@RequestMapping("/gotoadd/{deptId}.html")
	public String gotoAdd(ModelMap model, @PathVariable("deptId") String deptId) {
		SysDept dept = sysDeptService.findByDeptId(deptId);
		model.addAttribute("dept", dept);
		//获取一级部门信息
		//判断地级和分局
		if(deptId.indexOf("00")==4){
			deptId=deptId.substring(0, 8);
		}
		else
		{
			deptId=deptId.substring(0, 6);
		}
		//deptId = deptId.length()>=8?deptId.substring(0, 8):deptId.substring(0, 6);
		SysDept deptTop = sysDeptService.findByDeptId(deptId);
		model.addAttribute("deptTop", deptTop);
		return "/dept/user_add";
	}

	/**
	 * 跳转到编辑
	 */
	@RequestMapping("/gotoedit/{policeNum}.html")
	public String gotoEdit(ModelMap model,
			@PathVariable("policeNum") String policeNum) {
		SysUser user = sysUserService.findByPoliceNum(policeNum);
		SysDept dept = sysDeptService.findByDeptId(user.getDeptId());
		model.addAttribute("dept", dept);
		//获取县分局一级部门信息
		//判断地级和分局
		String deptId = user.getDeptId();
		if(deptId.indexOf("00")==4){
			deptId=deptId.substring(0, 6);
		}
		else
		{
			deptId=deptId.substring(0, 6);
		}
		//deptId = deptId.length()>=8?deptId.substring(0, 8):deptId.substring(0, 6);
		SysDept deptTop = sysDeptService.findByDeptId(deptId);
		model.addAttribute("deptTop", deptTop);
		model.addAttribute("user", user);
		return "/dept/user_edit";
	}

	/**
	 * 新增用户
	 * 
	 * @param model
	 * @param user
	 */
	@RequestMapping("/saveadd")
	public void addUser(ModelMap model, SysUser user) {
		String password = new Blowfish(Constant.ENCRYPT_SALT)
				.encryptString("123456");
		user.setPassword(password);
		int result = sysUserService.saveAddUser(user);
		model.addAttribute("result", result);
		model.addAttribute(
				Constant.LOG_RECORD,
				new SysOperLog(Constant.CREATE, "新增用户",
						ConstantMap.MODUAL_SYS_MGR, "新增警号为"
								+ user.getPoliceNum() + "的用户"));

	}

	@RequestMapping("/saveedit")
	public void editUser(ModelMap model, SysUser user) {
		int result = sysUserService.saveEditUser(user);
		model.addAttribute("result", result);
		model.addAttribute(
				Constant.LOG_RECORD,
				new SysOperLog(Constant.UPDATE, "新增编辑",
						ConstantMap.MODUAL_SYS_MGR, "编辑警号为"
								+ user.getPoliceNum() + "的用户信息"));
	}

	@RequestMapping("/resetpwd/{policeNum}")
	public void resetPwd(@PathVariable("policeNum") String policeNum,
			ModelMap model) {
		String password = new Blowfish(Constant.ENCRYPT_SALT)
				.encryptString("123456");
		// System.out.println(password);
		sysUserService.resetPwd(policeNum, password);
		model.addAttribute(Constant.LOG_RECORD, new SysOperLog(Constant.UPDATE,
				"重置密码", ConstantMap.MODUAL_SYS_MGR, "重置警号为" + policeNum
						+ "的密码"));
	}

	@RequestMapping("/updatepwd")
	public void updatepwd(HttpServletRequest request, SysUser user,
			ModelMap model) {
		SysUser user1 = (SysUser) request.getSession().getAttribute(
				Constant.LOGIN_USER);
		SysUser user2 = sysUserService
				.findUserByPoliceNum(user1.getPoliceNum());
		user.setPoliceNum(user1.getPoliceNum());
		if (user == null
				|| !user.getPassword().equals(
						new Blowfish(Constant.ENCRYPT_SALT).decryptString(user2
								.getPassword()))) {
			model.addAttribute("result", 0);
		} else {
			String password = new Blowfish(Constant.ENCRYPT_SALT)
					.encryptString(user.getNewPassword());
			user.setPassword(password);
			sysUserService.updatePassword(user);
			model.addAttribute("result", 1);
		}
	}

	/**
	 * 根据 编号 删除 人员信息
	 * 
	 * @param dbId
	 *            编号
	 */
	@RequestMapping(value = "/delete/{policenum}/{deptId}/{pageIndex}")
	public void delete(@PathVariable("policenum") String policenum,
			@PathVariable("deptId") String deptId,
			@PathVariable("pageIndex") int pageIndex, ModelMap model) {
		int result = sysUserService.deleteUserByDbId(policenum);
		if (result == 1) {
			model.addAttribute("result", 1);
		} else {
			model.addAttribute("result", 0);
		}
		model.addAttribute(Constant.LOG_RECORD, new SysOperLog(Constant.DELETE,
				"人员删除", ConstantMap.MODUAL_SYS_MGR, "人员警号" + policenum));
	}

	/**
	 * 人名模糊匹配
	 * 
	 * @param userName
	 * @param limit
	 *            最多显示条数
	 * @param model
	 */
	@RequestMapping("info")
	public void autocompleteUserInfo(@RequestParam("userName") String userName,
			@RequestParam("limit") int limit, ModelMap model) {
		if (StringUtils.hasText(userName)) {
			try {
				userName = URLDecoder.decode(userName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			List<SysUser> user = sysUserService.findUserByFuzzyName(userName,
					limit);
			model.addAttribute("users", user);
		}
	}

	/**
	 * 根据本部门人员姓名带出相关信息
	 * 
	 * @param userName
	 */
	@RequestMapping("/savecase/username")
	public void autocompleteUserName(HttpServletRequest request, ModelMap model) {
		String xm = request.getParameter("xm");
		SysUser curUser = (SysUser) request.getSession().getAttribute(
				Constant.LOGIN_USER);
		String deptId = curUser.getDeptId();
		if (!"".equals(xm) && null != xm) {
			try {
				xm = URLDecoder.decode(xm, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			List<SysUser> users = sysUserService.findUserByXm(xm,deptId);
			model.addAttribute("users", users);
		}
	}
	
	/**
	 * 根据本部门人员姓名带出相关信息
	 * 
	 * @param userName
	 */
	@RequestMapping("/all/username")
	public void autocompleteAllUserName(HttpServletRequest request, ModelMap model) {
		String xm = request.getParameter("xm");
		if (!"".equals(xm) && null != xm) {
			try {
				xm = URLDecoder.decode(xm, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			List<SysUser> users = sysUserService.findAllUserByXm(xm);
			model.addAttribute("users", users);
		}
	}
	
	@RequestMapping("/listggo/{deptId}.json")
	public ModelAndView getAllUser1(HttpServletRequest request, SysUser user, @PathVariable("deptId") String deptId) {
		ModelAndView modelAndView = new ModelAndView(Constants.RESPONSE_VIEW);
		String pageIndex1 = request.getParameter("page");
		if(StringUtil.isNull(pageIndex1))
		{
			pageIndex1 = "1";
		}
		int pageIndex = Integer.parseInt(pageIndex1);
		String policeNum = request.getParameter("policeNum");
		String name = request.getParameter("name");
		
		
		if (StringUtils.hasLength(name)) {
			user.setName(name);
		}
		if (StringUtils.hasLength(policeNum)) {
			user.setPoliceNum(policeNum);
		}
		
		if ("320500".equals(deptId))
			user.setCascade("1");
		
		Pagination<SysUser> page = sysUserService.findByPage(pageIndex, user,
				deptId);
		modelAndView.addObject("page",page);
		modelAndView.addObject("rows",page.getList());
		modelAndView.addObject("total",page.getTotalRows());
		return modelAndView;
	}
	
	
}
